{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "source": [
    "class Washer():\r\n",
    "    def wash(self):\r\n",
    "        print(\"洗衣服\")"
   ],
   "outputs": [],
   "metadata": {
    "collapsed": true
   }
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "source": [
    "# 创建对象\r\n",
    "haier = Washer()\r\n",
    "print(haier)"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "<__main__.Washer object at 0x00000255029B80B8>\n"
     ]
    }
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "source": [
    "# 使用wash功能\r\n",
    "\r\n",
    "haier.wash()"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "洗衣服\n"
     ]
    }
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "source": [
    "# 1. 定义类\r\n",
    "class Washer():\r\n",
    "    def wash(self):\r\n",
    "        print('我会洗衣服')\r\n",
    "        # <__main__.Washer object at 0x0000024BA2B34240>\r\n",
    "        print(self)\r\n",
    "\r\n",
    "\r\n",
    "# 2. 创建对象\r\n",
    "haier1 = Washer()\r\n",
    "# <__main__.Washer object at 0x0000018B7B224240>\r\n",
    "print(haier1)\r\n",
    "# haier1对象调用实例方法\r\n",
    "haier1.wash()"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "<__main__.Washer object at 0x00000255029AF4A8>\n",
      "我会洗衣服\n",
      "<__main__.Washer object at 0x00000255029AF4A8>\n"
     ]
    }
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "source": [
    "haier2 = Washer()\r\n",
    "# <__main__.Washer object at 0x0000022005857EF0>\r\n",
    "print(haier2)\r\n",
    "haier2.wash()"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "<__main__.Washer object at 0x00000255029B8198>\n",
      "我会洗衣服\n",
      "<__main__.Washer object at 0x00000255029B8198>\n"
     ]
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "source": [
    "haier1.width = 500\r\n",
    "haier1.height = 800"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "source": [
    "print(f'haier1洗衣机的宽度是{haier1.width}')\r\n",
    "print(f'haier1洗衣机的高度是{haier1.height}')"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "haier1洗衣机的宽度是500\n",
      "haier1洗衣机的高度是800\n"
     ]
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 类里面获取对象属性"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "source": [
    "# 定义类\r\n",
    "class Washer():\r\n",
    "    def print_info(self):\r\n",
    "        # 类里面获取实例属性\r\n",
    "        print(f'haier1洗衣机的宽度是{self.width}')\r\n",
    "        print(f'haier1洗衣机的高度是{self.height}')\r\n",
    "\r\n",
    "# 创建对象\r\n",
    "haier1 = Washer()\r\n",
    "\r\n",
    "# 添加实例属性\r\n",
    "haier1.width = 500\r\n",
    "haier1.height = 800\r\n",
    "\r\n",
    "haier1.print_info()"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "haier1洗衣机的宽度是500\n",
      "haier1洗衣机的高度是800\n"
     ]
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "source": [
    "class Washer():\r\n",
    "    \r\n",
    "    # 定义初始化功能的函数\r\n",
    "    def __init__(self):\r\n",
    "        # 添加实例属性\r\n",
    "        self.width = 500\r\n",
    "        self.height = 800\r\n",
    "\r\n",
    "\r\n",
    "    def print_info(self):\r\n",
    "        # 类里面调用实例属性\r\n",
    "        print(f'洗衣机的宽度是{self.width}, 高度是{self.height}')\r\n",
    "\r\n",
    "\r\n",
    "haier1 = Washer()\r\n",
    "haier1.print_info()"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "洗衣机的宽度是500, 高度是800\n"
     ]
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "source": [
    "print(haier1.width)\r\n",
    "\r\n",
    "# 类外面修改\r\n",
    "haier1.width = 30\r\n",
    "haier1.height = 80\r\n",
    "haier1.print_info()"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "500\n",
      "洗衣机的宽度是30, 高度是80\n"
     ]
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "source": [
    "class Washer():\r\n",
    "    def __init__(self, width, height):\r\n",
    "        self.width = width\r\n",
    "        self.height = height\r\n",
    "\r\n",
    "    def print_info(self):\r\n",
    "        print(f'洗衣机的宽度是{self.width}')\r\n",
    "        print(f'洗衣机的高度是{self.height}')\r\n",
    "\r\n",
    "\r\n",
    "haier1 = Washer(10, 20)\r\n",
    "haier1.print_info()\r\n",
    "\r\n",
    "\r\n",
    "haier2 = Washer(30, 40)\r\n",
    "haier2.print_info()"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "洗衣机的宽度是10\n",
      "洗衣机的高度是20\n",
      "洗衣机的宽度是30\n",
      "洗衣机的高度是40\n"
     ]
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "source": [
    "class Washer():\r\n",
    "    def __init__(self, width, height):\r\n",
    "        self.width = width\r\n",
    "        self.height = height\r\n",
    "\r\n",
    "    def __str__(self):\r\n",
    "        return '这是海尔洗衣机的说明书'\r\n",
    "\r\n",
    "\r\n",
    "haier1 = Washer(10, 20)\r\n",
    "# 这是海尔洗衣机的说明书\r\n",
    "print(haier1)"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "这是海尔洗衣机的说明书\n"
     ]
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "source": [
    "class Washer():\r\n",
    "    def __init__(self, width, height):\r\n",
    "        self.width = width\r\n",
    "        self.height = height\r\n",
    "\r\n",
    "haier1 = Washer(10, 20)\r\n",
    "\r\n",
    "# <__main__.Washer object at 0x0000026118223278>对象已经被删除\r\n",
    "del haier1"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "source": [
    "class Washer():\r\n",
    "    def __init__(self, width, height):\r\n",
    "        self.width = width\r\n",
    "        self.height = height\r\n",
    "\r\n",
    "    def __del__(self):\r\n",
    "        print(f'{self}对象已经被删除')\r\n",
    "\r\n",
    "\r\n",
    "haier1 = Washer(10, 20)\r\n",
    "\r\n",
    "# <__main__.Washer object at 0x0000026118223278>对象已经被删除\r\n",
    "del haier1"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "<__main__.Washer object at 0x00000255029B85C0>对象已经被删除\n"
     ]
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "source": [
    "def get_name_of_obj(obj, except_word=\"\"):\r\n",
    "    for name, item in globals().items():\r\n",
    "        if item == obj and name != except_word:\r\n",
    "            print(name)\r\n",
    "            print(type(name))\r\n",
    "            return name\r\n",
    "\r\n",
    "class Washer():\r\n",
    "    def __init__(self, width, height):\r\n",
    "        self.width = width\r\n",
    "        self.height = height\r\n",
    "\r\n",
    "    def __str__(self) -> str:\r\n",
    "        return get_name_of_obj(self, except_word='item')\r\n",
    "\r\n",
    "    def __del__(self):\r\n",
    "        print(get_name_of_obj(self, except_word='item'), end='')\r\n",
    "        print('对象已经被删除')\r\n",
    "\r\n",
    "\r\n",
    "haier1 = Washer(10, 20)\r\n",
    "\r\n",
    "print(haier1)\r\n",
    "print(type(haier1))\r\n",
    "\r\n",
    "# <__main__.Washer object at 0x0000026118223278>对象已经被删除\r\n",
    "del haier1"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "haier1\n",
      "<class 'str'>\n",
      "haier1\n",
      "<class '__main__.Washer'>\n",
      "None对象已经被删除\n"
     ]
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [],
   "outputs": [],
   "metadata": {}
  }
 ],
 "metadata": {
  "kernelspec": {
   "name": "python3",
   "display_name": "Python 3.6.4 64-bit ('base': conda)"
  },
  "language_info": {
   "name": "python",
   "version": "3.6.4",
   "mimetype": "text/x-python",
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "pygments_lexer": "ipython3",
   "nbconvert_exporter": "python",
   "file_extension": ".py"
  },
  "interpreter": {
   "hash": "a8f61be024eba58adef938c9aa1e29e02cb3dece83a5348b1a2dafd16a070453"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}